package com.neuedu.his.service;

import com.neuedu.his.dao.UserDao;
import com.neuedu.his.dao.UserRoleDao;
import com.neuedu.his.entity.User;
import com.neuedu.his.entity.UserRole;
import com.neuedu.his.framework.BaseService;
import com.neuedu.his.framework.PageInfo;
import com.neuedu.util.JDBCUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       UserService
 * 创建时间：  2020/12/23  8:52
 * 描述 :     用户测试类
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class UserService implements BaseService<User> {
    private UserDao userDao = new UserDao();
    private UserRoleDao userRoleDao = new UserRoleDao();

    @Override
    public List<User> queryList(HttpServletRequest request) {

        System.out.println("2020年12月23日09:26:13");
        String sql = " select * from user ";
        return userDao.selectList(sql);
    }


    /**
     * 分页查询
     *
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public PageInfo queryListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        PageInfo pageInfo = getPageInfo(request);

        String realname = request.getParameter("realname");


        if (realname == null) {
            realname = "";
        }

        realname = "%" + realname.trim() + "%";


        String sql = " select * from user  where active = 1 and realname like ?   limit ?  , ?  ";

        int start = (pageInfo.getPageNo() - 1) * pageInfo.getPageSize();
        List<User> list = userDao.selectList(sql, realname, start, pageInfo.getPageSize());

        String sql2 = "  select count(1) from user    where active = 1 and realname like ?   ";
        long count = userDao.selectCount(sql2, realname);

        pageInfo.setList(list);
        pageInfo.setTotalCount((int) count);

        return pageInfo;
    }

    @Override
    public User queryById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public boolean edit(User user) {
        return false;
    }


    /**
     * 逻辑删除，没有真正删除数据
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        return userDao.deleteById(id);
    }

    @Override
    public boolean save(User user) {
        return userDao.insert(user);
    }

    @Override
    public boolean batchdel(String[] ids) throws Exception {
        return false;
    }

    public boolean update(User user) {
        return userDao.update(user);
    }

    public boolean grantRole(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String roleIdParms = request.getParameter("roleId");


        //维护user_role表， 先删后插如
        String sql = "delete from user_role where user_id = ?";
        userRoleDao.delete(sql, userId);

        boolean success = true;
        String[] roleIds = roleIdParms.split(",");
        for (String roleId : roleIds) {
            UserRole userRole = new UserRole(Integer.parseInt(userId), Integer.parseInt(roleId));
            success = success && userRoleDao.insert(userRole);

        }

        return success;
    }

    public List<UserRole> getUserRols(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        return userRoleDao.selectUserRole(Integer.parseInt(userId));
    }


    /**
     * 根据 科室、 挂号级别查询医生
     * @param request
     * @return
     */
    public List<User> queryDoctor(HttpServletRequest request) {

        String deptId = request.getParameter("deptId");
        String registLevel = request.getParameter("registLevel");
        String sql = "select * from user where active = 1 and dept_id = ? and regist_level = ?";
        return userDao.selectList(sql, deptId, registLevel);
    }
}
